package cn.itcast.shop.categorysecond.adminaction;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.shop.category.service.CategoryService;
import cn.itcast.shop.category.vo.Category;
import cn.itcast.shop.categorysecond.service.CategorySecondService;
import cn.itcast.shop.categorysecond.vo.CategorySecond;
import cn.itcast.shop.utils.PageBean;

public class AdminCategorySecondAction extends ActionSupport implements ModelDriven<CategorySecond> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CategorySecond categorySecond = new CategorySecond();
	@Override
	public CategorySecond getModel() {
		return categorySecond;
	}
	//注入一级分类的Service
	private CategoryService categoryService;
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	private CategorySecondService categorySecondService;
	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}
	
	private Integer page;
	
	public void setPage(Integer page) {
		this.page = page;
	}

	public String findAllByPage(){
		PageBean<CategorySecond> pageBean = categorySecondService.findByPage(this.page);
		
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findAllByPage";
	}
	
	/**
	 * 跳转到添加二级分类
	 * @return
	 */
	public String addPage(){
		//查询所有的一级分类
		List<Category> cList = categoryService.findAll();
		ActionContext.getContext().getValueStack().set("cList", cList);
		//页面跳转：
		return "addPageSuccess";
	}
	
	/**
	 * 保存二级分类的方法
	 * @return
	 */
	public String save(){
		categorySecondService.save(categorySecond);
		
		return "saveSuccess";
	}
	
	/**
	 * 删除二级分类
	 * @return
	 */
	public String delete(){
		categorySecond = categorySecondService.findByCsid(categorySecond.getCsid());
		categorySecondService.delete(categorySecond);
		return "deleteSuccess";
	}
	
	/**
	 * 编辑二级分类的方法
	 * @return
	 */
	public String edit(){
		//根据二级分类id查询二级分类的对象
		categorySecond = categorySecondService.findByCsid(categorySecond.getCsid());
		//查询所有的一级分类。
		List<Category> cList = categoryService.findAll();
		ActionContext.getContext().getValueStack().set("cList", cList);
		return "editSuccess";
	}
	
	/**
	 * 修改二级分类的方法
	 * @return
	 */
	public String update(){
		categorySecondService.update(categorySecond);
		return "updateSuccess";
	}
	
}





















